node{
    def releaseVersion = '1.0'
    def serviceName = "${projectName}"
    def registryHost = '106.14.141.132:80'
    def gradle = "${gradleHome}/bin/gradle"

    stage('clean workspace') {
        cleanWs()
    }

    stage('git clone common') {
        sh 'mkdir common'
        dir('./common') {
            git credentialsId: 'gitee_credential', url: 'https://git.oschina.net/sidecar/common.git'
        }
    }

    stage("git clone ${serviceName}") {
        sh "mkdir ${serviceName}"
        dir("./${serviceName}"){
            git credentialsId: 'gitee_credential', url: "https://git.oschina.net/sidecar/${serviceName}.git" 
        }
        dir("./${serviceName}/src/main/resources") {
            sh "sh /data/shells/generateDockerfile.sh \$(pwd) ${serviceName}-${releaseVersion}"
        }
    }

    stage('gradle build') {
        dir("./${serviceName}"){
            sh 'echo $GRADLE_HOME   , $PATH'
            sh "${gradle} clean buildDocker -x test"
        }
    }

    stage('clean images on server') {
        sh """
            imgs=`sudo docker images|grep '<none>'|awk '{print \$3}'`
            echo 'imgs value is:::::'\$imgs;
            if [ -n "\$imgs" ];
                then
                    for i in \$imgs;
                    do
                        sudo docker rmi -f \$i
                    done
            else
                echo 'there are no images to clean. '
            fi;
        """
    }

    stage('push image') {
        sh """
            sudo docker tag sidecar/${serviceName}:${releaseVersion} ${registryHost}/sidecar/${serviceName}:${BUILD_NUMBER}
            sudo docker tag sidecar/${serviceName}:${releaseVersion} ${registryHost}/sidecar/${serviceName}
            sudo docker push ${registryHost}/sidecar/${serviceName}:${BUILD_NUMBER}
            sudo docker push ${registryHost}/sidecar/${serviceName}
        """
    }

    stage('Publish build info') {
        println 'buid image success. '
    }
}